//
//  SysemTipView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/29.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class SysemTipView: UIView {
    
    // scrollView
    private let scrollView = UIScrollView()
    
    // 赋值
    public var notificationInfos:[NotificationInfo]? {
        didSet {
            setNeedsLayout()
        }
    }
    // 回调
    public var complete:((_ item:Int) -> Void)?
    
    override func layoutSubviews() {
        guard let infos = notificationInfos else {
            return
        }
        // 高度
        var tempHeight:CGFloat = 0
        // 宽度
        let w = frame.width - MARGIN * 2
        for i in 0..<infos.count {
            // 背景颜色
            let backround = UIButton()
            backround.tag = i
            backround.layer.cornerRadius = 8
            backround.layer.masksToBounds = true
            // 公告
            let tipView = UIButton()
            tipView.setTitle("公告", for: .normal)
            tipView.isUserInteractionEnabled = false
            tipView.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            tipView.setTitleColor(UIColor.white, for: .normal)
            tipView.contentEdgeInsets = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
            tipView.layer.cornerRadius = 5
            tipView.layer.masksToBounds = true
            tipView.backgroundColor = Palette.colorFromRGB(81, green: 105, blue: 242)
            tipView.frame = CGRect(x: MARGIN, y: MARGIN, width: MARGIN44, height: MARGIN20)
            backround.addSubview(tipView)
            // 时间
            let timeView = UILabel(title: "测试", color: UIColor.gray, size: 14, align: .right)
            timeView.text = infos[i].createtime
            timeView.isUserInteractionEnabled = false
            let timeW = frame.width - tipView.frame.maxX - MARGIN
            timeView.frame = CGRect(x: tipView.frame.maxX, y: MARGIN, width: timeW, height: MARGIN20)
            backround.addSubview(timeView)
            // 下划线
            let line1 = UIView()
            line1.backgroundColor = COLOR_LINE
            line1.frame = CGRect(x: MARGIN, y: tipView.frame.maxY + MARGIN, width: w, height: 1)
            backround.addSubview(line1)
            // 公告内容
            let contentView = UILabel(title: "测试", color: UIColor.black)
            contentView.numberOfLines = 2
            contentView.text = infos[i].content
            contentView.isUserInteractionEnabled = false
            contentView.font = UIFont.boldSystemFont(ofSize: 17)
            contentView.frame = CGRect(x: MARGIN, y: line1.frame.maxY + MARGIN, width: w, height: MARGIN80)
            backround.addSubview(contentView)
            // 下划线
            let line2 = UIView()
            line2.backgroundColor = COLOR_LINE
            line2.frame = CGRect(x: MARGIN, y: contentView.frame.maxY + MARGIN10, width: w, height: 1)
            backround.addSubview(line2)
            // 查看详情
            let detailView = UILabel(title: "查看详情", color: UIColor.gray, size: 13)
            detailView.isUserInteractionEnabled = false
            detailView.frame = CGRect(x: MARGIN, y: line2.frame.maxY, width: w, height: MARGIN50)
            backround.addSubview(detailView)
            // 箭头
            let arrowView = UIImageView(image: UIImage(named: "arrow_6x11"))
            let arrowX = frame.width - arrowView.bounds.width - MARGIN
            let arrowY = line2.frame.maxY + (MARGIN50 - arrowView.bounds.height) * 0.5
            arrowView.frame.origin = CGPoint(x: arrowX, y: arrowY)
            backround.addSubview(arrowView)
            // 背景颜色
            backround.backgroundColor = UIColor.white
            backround.frame = CGRect(x: 0, y: tempHeight, width: frame.width, height: detailView.frame.maxY)
            scrollView.addSubview(backround)
            addSubview(scrollView)
            // 每条高度
            tempHeight += detailView.frame.maxY + MARGIN10
            // 设置scrollview
            scrollView.showsVerticalScrollIndicator = false
            scrollView.contentSize = CGSize(width: frame.width, height: tempHeight)
            scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
            // 添加点击事件
            backround.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
        }
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        if let callback = complete {
            callback(btn.tag)
        }
    }
}
